package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.easyViewHolder.EditSearchHolder;
import com.dingwei.superiordiary.bean.SearchBean;
import com.dingwei.superiordiary.mvp.presenter.SearchEditPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.KeyboardUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/1.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class SearchEditActivity extends BaseActivity implements Contract.SearchEditView {

    SearchEditPresenter searchEditPresenter = new SearchEditPresenter(this);
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.tv_cancel)
    TextView tvCancel;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;

    private String string = "";

    RecyclerArrayAdapter mAdapter = new RecyclerArrayAdapter(context) {
        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new EditSearchHolder(parent);
        }
    };

    @Override
    protected int getContentViewId() {
        return R.layout.activity_edit_search;
    }

    @Override
    protected void initData() {
        searchEditPresenter.initData();
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setNestedScrollingEnabled(false);

        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int i) {
                String s = (String) mAdapter.getItem(i);
                Intent intent = new Intent(getApplicationContext(), NewSuperiorProductsActivity.class);
                intent.putExtra("keyword", s);
                intent.putExtra("activity_title", "搜索结果");
                startActivity(intent);
            }
        });



    }

    /**
     * 搜索框监听
     */
    TextView.OnEditorActionListener listener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                KeyboardUtils.hideSoftInput(BaseActivity.context);
                String keyword = etSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(keyword)) {
                    Intent intent = new Intent(BaseActivity.context, NewSuperiorProductsActivity.class);
                    intent.putExtra("keyword", keyword);
                    intent.putExtra("activity_title", "搜索结果");
                    startActivity(intent);
                }
            }
            return false;
        }
    };

    @Override
    protected void initListener() {
        etSearch.setOnEditorActionListener(listener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_cancel)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setData(SearchBean bean) {
        mAdapter.addAll(bean.getData().getHot());
    }
}
